﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ProjectManageSystemApi.Models;
using ProjectManageSystemApi.Services;
using ProjectManageSystemApi.Utilities;

namespace ProjectManageSystemApi.Controllers
{
    [Authorize]
    [Route("api/[controller]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = Grouping.GroupName_v1)]
    public class CostTypeController : Controller
    {
        readonly ICostTypeServices _costType;
        readonly IUserServices _user;
        public CostTypeController(ICostTypeServices costType, IUserServices user)
        {
            _costType = costType;
            _user = user;
        }

        [HttpPost, Route("Create")]
        public ApiResult Create(CostTypeDto data)
        {
            if (data == null || string.IsNullOrWhiteSpace(data.Name))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            #region 从token获取userid
            string token = Request.GetToken();
            if (string.IsNullOrWhiteSpace(token))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (!_user.GetUserIdByToken(token, out long userId))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            #endregion
            data.Id = 0;
            if (_costType.HasExistName(data.Name))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PROJECT_COST_TYPE_NAME_EXIST);
            }
            bool r = _costType.Create(data, userId);
            if (!r)
            {
                return ApiResultStatus.Fail(ApiResultCodes.OPERATION_FAILED);
            }
            return ApiResultStatus.Success();
        }

        [HttpPost, Route("Update")]
        public ApiResult Update(CostTypeDto data)
        {
            if (data == null || string.IsNullOrWhiteSpace(data.Name) || data.Id == 0)
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (!_costType.HasExistId(data.Id))
            {
                return ApiResultStatus.Fail(ApiResultCodes.DATA_NOT_EXIST);
            }
            if (_costType.HasExistName(data.Name, data.Id))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PROJECT_COST_TYPE_NAME_EXIST);
            }
            bool r = _costType.Update(data);
            if (!r)
            {
                return ApiResultStatus.Fail(ApiResultCodes.OPERATION_FAILED);
            }
            return ApiResultStatus.Success();
        }

        [HttpPost, Route("Delete")]
        public ApiResult Delete([FromQuery] int id)
        {
            if (id == null)
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (!_costType.HasExistId(id))
            {
                return ApiResultStatus.Fail(ApiResultCodes.DATA_NOT_EXIST);
            }
            if (!_costType.Delete(id))
            {
                return ApiResultStatus.Fail(ApiResultCodes.OPERATION_FAILED);
            }
            return ApiResultStatus.Success();
        }
        
        [HttpPost,Route("GetAll")]
        public ApiResult GetAll(PageBo<CostTypeParams> pageBo) 
        {
            if (pageBo == null) 
            {
                pageBo = new PageBo<CostTypeParams>();
            }
            if (pageBo.Param == null) 
            {
                pageBo.Param = new CostTypeParams();
            }
            return ApiResultStatus.Success(_costType.GetAll(pageBo));
        }
        
    }
}
